import { text, integer } from 'drizzle-orm/sqlite-core/columns';
import { sqliteTable, index } from 'drizzle-orm/sqlite-core';

import { auditSchema } from './audit';
import { table as drivers } from './drivers';
export const tableName = 'feeds';

export const definition = {
  id: text('id').primaryKey(),
  uid: text().notNull(),
  url: text().notNull(),
  title: text().notNull(),
  driverId: text().references(() => drivers.id).notNull(),
  description: text(),
  regex: text(),
  lastBuildDate: integer('createdAt', {
    mode: "timestamp"
  }),
  cron: text(),
  hasError: integer('hasError', { mode: 'boolean' }),
  errorMsg: text(),
  isLoading: integer('isLoading', { mode: 'boolean' }),
  unreadCount: integer('unreadCount'),
  markAsRead: integer('markAsRead', { mode: 'boolean' }),
  disabled: integer('disabled', { mode: 'boolean' }),
  iconPath: text(),
};

export const table = sqliteTable(
  tableName,
  {
    ...definition,
    ...auditSchema,
  },
  (table) => [
    index('userFeedIndex').on(table.uid),
    index('feedDriverIndex').on(table.driverId),
  ]
);
